package com.Darkra1Zzz.controller;

import com.Darkra1Zzz.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author DarkraiZzz
 * @Time 2021/6/27 23:59
 * @Version 1.0
 */

@Controller
@RequestMapping("ajax")
public class AjaxController {
    @RequestMapping("ajax")
    public String ajax() {
        return "myajax";
    }

    //    @RequestMapping("ajax1")
//    public String ajax1(String name, int age, HttpServletResponse response) throws IOException {
//        String str="姓名： "+"name"+" 年龄： "+"age";
//        response.getWriter().print(str);
//        return "null";
//    }
//    @RequestMapping("ajax1")
//    public String ajax1(@RequestParam("name")String name, @RequestParam("age")int age, PrintWriter printWriter) throws IOException {
//        String str = "姓名： " + name + " 年龄： " + age;
//        printWriter.print(str);
//        return "null";
//    }

    @RequestMapping("ajax2")
    @ResponseBody
    public User ajax2(String name,int age){
        User user=new User();
        user.setName(name);
        user.setId(age);
        return user;
    }

    @RequestMapping("ajax3")
    @ResponseBody
    public List<User> ajax3(){
        List<User> userList=new ArrayList<>();
        for (int i=0;i<4;i++){
            User user=new User();
            user.setName("姓名"+i);
            user.setId(i);
            userList.add(user);
        }
        return userList;
    }
}
